/**
 * 
 */
package com.cap.cfae;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.eos.foundation.data.DataObjectUtil;
import com.eos.foundation.database.DatabaseUtil;
import com.eos.system.annotation.Bizlet;

import commonj.sdo.DataObject;

/**
 * @author zp
 * @date 2018-02-05 21:59:08
 *
 */
@Bizlet("")
public class TagService {
	
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	
	@Bizlet("add")
	public Map<String,String>  addTag(String tagName)
	{
		Map<String,String> result=new HashMap<String, String>();
		 DataObject  tag = DataObjectUtil.createDataObject("com.cap.cfae.cfae.Tag");
		 tag.set("id", getUUID());
		 tag.set("tagName",tagName);
		 tag.set("createDate", format.format(new Date()));
		 tag.set("userId","kdjfhgbjbdf");
		 try{
			 DatabaseUtil.insertEntity("default", tag);
			 result.put("code", "0");
			 result.put("msg", "添加标签成功");
		 }catch(Exception e){
			 result.put("code", "1");
			 result.put("msg", "添加标签失败");
			 e.printStackTrace();
		 }
		 
		 return result;
		
	}
	
	public String getUUID()
	{
		String uuid = UUID.randomUUID().toString(); 
		uuid = uuid.replace("-", "");    
		return uuid;
	}
	
	@Bizlet("update")
	public Map<String,String> updateTag(String id,String tagName)
	{
		Map<String,String> result=new HashMap<String, String>();
		DataObject  tag = DataObjectUtil.createDataObject("com.cap.cfae.cfae.Tag");
		tag.set("id",id);
		tag.set("tagName",tagName);
		tag.set("createDate", format.format(new Date()));
		tag.set("userId","kdjfhgbjbdf");
		 try{
			 DatabaseUtil.updateEntity("default", tag);
			 result.put("code", "0");
			 result.put("msg", "更新标签成功");
		 }catch(Exception e){
			 result.put("code", "1");
			 result.put("msg", "更新标签失败");
			 e.printStackTrace();
		 }
		 
		 return result;		
	}
	@Bizlet("delete")
	public Map<String,String> deleteTagById(String id)
	{
		Map<String,String> result=new HashMap<String, String>();
		DataObject  tag = DataObjectUtil.createDataObject("com.cap.cfae.cfae.Tag");
		tag.set("id",id);
		 try{
			 DatabaseUtil.deleteEntity("default", tag);
			 result.put("code", "0");
			 result.put("msg", "删除标签成功");
		 }catch(Exception e){
			 result.put("code", "1");
			 result.put("msg", "删除标签失败");
			 e.printStackTrace();
		 }
		 
		 return result;		
	}
	@Bizlet("list")
	public List<Map<String,String>> getListByTagId(String userId)
	{
		List<Map<String,String>> result=new ArrayList<Map<String,String>>();
		DataObject  tag = DataObjectUtil.createDataObject("com.cap.cfae.cfae.Tag");
		tag.set("userId",userId);
		DataObject[]  tags=DatabaseUtil.queryEntitiesByTemplate("default", tag);
		for(int i=0;i<tags.length;i++)
		{
			Map<String,String> map=new HashMap<String, String>();
			DataObject tagTemp=tags[i];
			String tagId=tagTemp.getString("id");
			DataObject  tagBond = DataObjectUtil.createDataObject("com.cap.cfae.cfae.TagBonds");
			tagBond.set("tagId",tagId);
			int count=DatabaseUtil.count("default", tagBond);
			map.put("tagName", tagTemp.getString("tagName"));
			map.put("createDate", tagTemp.getString("createDate"));
			map.put("modifyDate", tagTemp.getString("modifyDate"));
			map.put("count", String.valueOf(count));
			result.add(map);
			
		}
		return  result;
	}
	
//	public List<Map<String,String>> getTagList(String userId)
//	{
//		
//	}

}
